//
//  JZHAddViewController.m
//  极致荟
//
//  Created by hetao on 14-5-12.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHAddViewController.h"
#import "JZHMyPorfileTableViewCell.h"
#import <ShareSDK/ShareSDK.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "JZHZBRViewController.h"
@interface JZHAddViewController ()
{
    NSArray *arr;
}
@end

@implementation JZHAddViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    arr=[[NSArray alloc]initWithObjects:@"发送邀请到微信朋友圈", @"邀请好友",@"邀请新浪微博好友",@"邀请手机通讯录联系人",@"扫一扫",nil];
    self.navigationItem.title=@"邀请好友";
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }

}

 - (void)leftDrawerButtonPress:(UIButton *)but
{
    [UINavigationController Pop:self];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tongxunlu
{
    //定义通讯录名字为addressbook
    ABAddressBookRef tmpAddressBook = nil;
    
    //根据系统版本不同，调用不同方法获取通讯录
    if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
        tmpAddressBook=ABAddressBookCreateWithOptions(NULL, NULL);
        dispatch_semaphore_t sema=dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(tmpAddressBook, ^(bool greanted, CFErrorRef error){
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
       
    }
    else
    {
        tmpAddressBook =ABAddressBookCreate();
    }
    //取得通讯录失败
    if (tmpAddressBook==nil) {
        return ;
    };
    //将通讯录中的信息用数组方式读出
    NSArray* tmpPeoples = (NSArray*)CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(tmpAddressBook));
    
    //遍历通讯录中的联系人
    for(id tmpPerson in tmpPeoples){
        
        //获取的联系人单一属性:First name
        NSString* tmpFirstName = (NSString*)CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonFirstNameProperty));
        NSLog(@"First name:%@", tmpFirstName);
    }
    
}
#pragma mark ---tableview代理---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return arr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    int i= [[NSNumber numberWithInteger:indexPath.row] intValue];
    static NSString *str=@"adf";
    JZHMyPorfileTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if(!cell)
    {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"JZHMyPorfileTableViewCell" owner:self options:nil] objectAtIndex:0];
        [cell.image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"邀%d",i]]];
        [cell.lable setText:arr[indexPath.row]];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            [self share:23];
            break;
        case 1:
            [self share:22];
            break;
        case 2:
            [self share:1];
            break;
        case 3:
            [self share:19];
            break;
        case 4:
            [self.navigationController pushViewController:[[JZHZBRViewController alloc] init] animated:YES];
        default:
            break;
    }
}
///分享数据
- (void)share:(int)type
{
    id<ISSCAttachment> catt=[ShareSDK imageWithData:nil fileName:@"" mimeType:@""];
    id<ISSContent> iss=[ShareSDK content:@"这个还是在测试分析，别当真" defaultContent:@"高大上的APP，极致荟等待你的到来" image:nil title:@"高大上的APP，极致荟等待你的到来" url:@"http://www.sharesdk.cn" description:@"高大上的APP，极致荟等待你的到来" mediaType:SSPublishContentMediaTypeText locationCoordinate:nil];
    
    if(type !=22 && type!=23)
    {
        [ShareSDK shareContent:iss type:type authOptions:nil shareOptions:nil statusBarTips:YES result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
            NSLog(@"%@",error.errorDescription);
        }];
        
    }else
    {
        [ShareSDK shareContent:iss type:ShareTypeWeixiSession authOptions:nil shareOptions:nil statusBarTips:YES result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
            NSLog(@"mmm%d %@  %d",[error errorCode],[error errorDescription],[error errorLevel]);
        }];
        
    }
    
    
    
}
@end
